﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using XISoft.AppCreator.Util.Common;
using System.IO;

namespace XISoft.AppCreator.Util.ConfigHelper
{
    public class ProjectConfig
    {

        private readonly static ConfigHelper<Project> config = new ConfigHelper<Project>(System.Environment.CurrentDirectory + @"\config\ProjectList.Config");

        public static List<Project> GetAllProject()
        {

            for (int i = config.Elements.Count - 1; i >= 0; i--)
                if (config.Elements[i].FilePath==null || !File.Exists(config.Elements[i].FilePath.ToString()))
                {
                    config.Elements.RemoveAt(i);
                }

            config.Save();
            return config.Elements;
        }

        public static Project getProjectByName(String Name)
        {
            try
            {
                foreach (Project p in config.Elements)
                    if (p.Name.Equals(Name))
                        return p;
            }
            catch (Exception e)
            {
                LogHelper.Trace(e);
            }
            throw new Exception("project No Fount");
        }
        public static void Add(Project p)
        {
            config.Add(p);
            config.Save();
        }

        public static void Remove(Project p)
        {
            config.Remove(p);
            File.Delete(p.FilePath);
            config.Save();
        }

        public static void Save()
        {
            config.Save();
        }
    }
}
